﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.DAL;


namespace DrugStorehouseMIS.SystemProtected
{
    public partial class durg_info_manager : Form
    {

        private Drug drugBLL;
        public durg_info_manager()
        {
            InitializeComponent();

            drugBLL = new Drug();
        }

      
        private void durg_info_manager_Load(object sender, EventArgs e)
        {          
            this.BindDrugType(comboBox2 );
            this.BindDrugType(comboBox3);
            this.BindMeasuringUnit();
        }
      
        private void tsBtnInHeaderNew_Click(object sender, EventArgs e)
        {
           add_drug form = new add_drug();
           form.Show();
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {           
            dataGridView1.DataSource = drugBLL.GetAllDrug(comboBox2.Text, "", "","");
        }

        private void btnInHeaderSearch_Click(object sender, EventArgs e)
        {
           // comboBox1.Text = "";
            Drug drug = new Drug();

            if (textBox1.Text.Trim() != "")
            {
                if (radioButton1.Checked)
                    dataGridView1.DataSource = drug.GetAllDrug(" ", textBox1.Text, " ", " ");
                if (radioButton2.Checked)
                    dataGridView1.DataSource = drug.GetAllDrug(" ", " ", textBox1.Text, " ");
                if (radioButton3.Checked)
                    dataGridView1.DataSource = drug.GetAllDrug(" ", " ", " ", textBox1.Text);

            }
            else
            {
                MessageBox.Show("请选择一种类型");
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            comboBox3.Enabled = false;
            textBox3.ReadOnly = true;
            textBox4.ReadOnly = true;
            textBox6.ReadOnly = true;
            textBox5.ReadOnly = true;
            textBox7.ReadOnly = true;
            comboBox4.Enabled = false;
            textBox9.ReadOnly = true;
            textBox10.ReadOnly = true;
            textBox11.ReadOnly = true;
            textBox12.ReadOnly = true;
            textBox13.ReadOnly = true;
            if (e.RowIndex > -1)
            {
                comboBox3 .Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
                comboBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
                textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();
                textBox10.Text = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
                textBox11.Text = dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString();
                textBox12.Text = dataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString();
                textBox13.Text = dataGridView1.Rows[e.RowIndex].Cells[12].Value.ToString();
            }
        }

        private void btnInHeaderReset_Click(object sender, EventArgs e)
        {
           // comboBox1.Text = "";
            comboBox2.Text = "";
            textBox1.Text = "";

        }

        private void tsBtnInHeaderEdit_Click(object sender, EventArgs e)
        {
            comboBox3.Enabled = true ;
            textBox3.ReadOnly = false;
            textBox4.ReadOnly = false;
            textBox6.ReadOnly = false;
            textBox5.ReadOnly = false;
            textBox7.ReadOnly = false;
            comboBox4.Enabled = true;
            textBox9.ReadOnly = false;
            textBox10.ReadOnly = false;
            textBox11.ReadOnly = false;
            textBox12.ReadOnly = false;
            textBox13.ReadOnly = false;

        }

        private void tsBtnInHeaderDelete_Click(object sender, EventArgs e)
        {  
            DialogResult result = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            Drug  drug = new Drug();

            if (result == DialogResult.OK)
            {
                drug.RemoveDrug(Convert .ToInt32 ( dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString ()) );//已通过存储过程里删除对应的drug
                
            }    
        }

        /// <summary>
        /// 绑定药品类型
        /// </summary>
        /// <param name="combox"></param>
        public void BindDrugType(ComboBox combox)
        {
            DrugType drugtype = new DrugType();
            IList<DrugType> list = drugtype.GetAllDrugType();
            foreach (DrugType obj in list)
            {
                combox.Items.Add(obj.DrugTypeName);

            }
            // cbxMeasuringUnit.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定单位
        /// </summary>
        public void BindMeasuringUnit()
        {
            MeasuringUnit measuringUnitBLL = new MeasuringUnit();
            IList<MeasuringUnit> list = measuringUnitBLL.GetAllMeasuringUnit();
            foreach (MeasuringUnit obj in list)
            {
                comboBox4.Items.Add(obj.MeasuringUnitName);
            }

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if(comboBox3 .Enabled )
            try
            {
                DrugDAL drug = new DrugDAL();

                int flaze = drug.Update(
                                Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString()),
                                comboBox3.Text,
                                textBox3.Text,
                                textBox4.Text,
                                textBox5.Text,
                                textBox6.Text,
                                textBox7.Text,
                                comboBox4.Text,
                                textBox9.Text,
                                Convert.ToInt32(textBox10.Text),
                                Convert.ToInt32(textBox11.Text),
                                Convert.ToSingle(textBox12.Text),
                                Convert.ToSingle(textBox13.Text)
                       );
                if (flaze > 0)
                {

                    comboBox3.Enabled = false;
                    textBox3.ReadOnly = true;
                    textBox4.ReadOnly = true;
                    textBox6.ReadOnly = true;
                    textBox5.ReadOnly = true;
                    textBox7.ReadOnly = true;
                    comboBox4.Enabled = false;
                    textBox9.ReadOnly = true;
                    textBox10.ReadOnly = true;
                    textBox11.ReadOnly = true;
                    textBox12.ReadOnly = true;
                    textBox13.ReadOnly = true;

                    MessageBox.Show("更新成功");                                                                                              
                
                }
            }catch (Exception d) 
            {
                MessageBox.Show(d.Message); 
            }                                                                                                                                                 
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}